package com.ok.common.ext

import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.ok.common.base.AppContext
import com.ok.common.widget.ShaderDrawable

//-------------------------drawableTop--------------------------

fun TextView.drawableTop(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(null, drawable, null, null)
}

fun TextView.drawableTop(@DrawableRes id: Int, size: Int) {
    drawableTop(id, size, size)
}

fun TextView.drawableTop(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableTop(id, drawable.minimumWidth, drawable.minimumHeight)
}

//-----------------------drawableBottom-------------------------

fun TextView.drawableBottom(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(null, null, null, drawable)
}

fun TextView.drawableBottom(@DrawableRes id: Int, size: Int) {
    drawableBottom(id, size, size)
}

fun TextView.drawableBottom(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableBottom(id, drawable.minimumWidth, drawable.minimumHeight)
}

//-------------------------drawableLeft--------------------------

fun TextView.drawableLeft(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(drawable, null, null, null)
}

fun TextView.drawableLeft(@DrawableRes id: Int, size: Int) {
    drawableLeft(id, size, size)
}

fun TextView.drawableLeft(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableLeft(id, drawable.minimumWidth, drawable.minimumHeight)
}

//-------------------------drawableRight--------------------------

fun TextView.drawableRight(@DrawableRes id: Int, width: Int, height: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawable.setBounds(0, 0, width, height)
    setCompoundDrawables(null, null, drawable, null)
}

fun TextView.drawableRight(@DrawableRes id: Int, size: Int) {
    drawableRight(id, size, size)
}

fun TextView.drawableRight(@DrawableRes id: Int) {
    val drawable: Drawable = ContextCompat.getDrawable(AppContext, id)!!
    drawableRight(id, drawable.minimumWidth, drawable.minimumHeight)
}

fun TextView.drawableClear() {
    this.setCompoundDrawables(null, null, null, null);
}

fun TextView.textMarquee() {
    this.ellipsize = TextUtils.TruncateAt.MARQUEE
    this.isSingleLine = true
    this.isSelected = true
    this.isFocusable = false
    this.isFocusableInTouchMode = false
    this.setHorizontallyScrolling(true)
}

//-------------------------动态设置shape--------------------------

fun TextView.shapeBackground(cornerRadius: Float = 3f.dp, @ColorRes colorResId: Int) {
    try {
        val color = ContextCompat.getColor(context, colorResId)
        val shape = createRoundedRectangle(color, cornerRadius)
        background = shape
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun TextView.shapeBackground(cornerRadius: Float = 3f.dp, hexColor: String) {
    try {
        val color = android.graphics.Color.parseColor(hexColor)
        val shape = createRoundedRectangle(color, cornerRadius)
        background = shape
        background = shape
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

private fun createRoundedRectangle(color: Int, cornerRadius: Float): GradientDrawable {
    val shape = GradientDrawable()
    shape.shape = GradientDrawable.RECTANGLE
    shape.setColor(color)
    shape.cornerRadius = cornerRadius
    return shape
}

fun TextView.alignText(fourChars: String) {
    val currentText = this.text.toString()
    // 计算空格的数量
    val spaceCount = (fourChars.length - currentText.length) / 2
    // 构建对齐文本
    val alignedText = StringBuilder()
    for (i in 0 until spaceCount) {
        alignedText.append(" ")
    }
    alignedText.append(fourChars)
    for (i in 0 until spaceCount) {
        alignedText.append(" ")
    }
    // 设置对齐后的文本到 TextView
    this.text = alignedText.toString()
}

fun TextView.applyGradientText(
    startColor: Int,
    midColor: Int? = null,
    endColor: Int,
    orientation: Orientation = Orientation.HORIZONTAL
) {
    val colors = mutableListOf<Int>().apply {
        add(startColor)
        midColor?.let { add(it) }
        add(endColor)
    }.toIntArray()
    val drawable = ShaderDrawable(this, colors, orientation)
    text = text
    setCompoundDrawables(null, null, null, null)
    compoundDrawablePadding = 0
    setDrawable(drawable)
}

private fun TextView.setDrawable(drawable: Drawable) {
    this@setDrawable.setCompoundDrawablesRelativeWithIntrinsicBounds(
        null, null, null, drawable
    )
}

enum class Orientation {
    HORIZONTAL,
    VERTICAL
}
